<?php 
namespace common\service;

use common\model\sell66\UserBalance;
use common\model\sell66\UserSteam;
use think\Cache;
use think\Request;
use common\service\act\CommissionService;
class AuthService{
	
	public static $instance = null;
	
	private function __construct(){
	}
	public static function getInstance(){
		if(self::$instance == null){
			self::$instance = new self();
		}
		return self::$instance;
	}
	
	public function saveSteamLoginInfo($steamUser){
		$userId = $steamUser['user_id'];
        $balance = UserBalance::getBalanceByUseId($userId);
		$steamId = $steamUser['steam_id'];
		$result['user_id'] = $steamUser['user_id'];
        $result['steam_id'] = $steamUser['steam_id'];
        $result['trade_off_url'] = isset($steamUser['trade_off_url']) ? $steamUser['trade_off_url'] : '';
        $result['created_time'] = $steamUser['created_time'];
		$result['updated_time'] = $steamUser['updated_time'];
		$result['communityvisibilitystate'] = $steamUser['communityvisibilitystate'];
		$result['avatarfull'] = $steamUser['avatarfull'];
		$result['personaname'] = $steamUser['personaname'];
		$result['avatar'] = $steamUser['avatar_icon'];
		$result['realname'] = $steamUser['realname'];
		$result['avatarmedium'] = $steamUser['avatarmedium'];
		$result['balance'] = isset($balance['balance']) ? $balance['balance'] : '0.00';
		$result['commission'] = true;
		$result['freezeBalance'] = isset($balance['freeze_balance']) ? $balance['freeze_balance'] : '0.00';
		$token = UserSteam::getTokenKey($userId,$steamId);
		$result['access_token'] = $token;
		$result['game_count'] = isset($steamUser['game_count']) ? $steamUser['game_count'] : 0;
		$result['friend_count'] = isset($steamUser['friend_count']) ? $steamUser['friend_count'] : 0;
		$result['player_level'] = isset($steamUser['player_level']) ? $steamUser['player_level'] : 0;
		Cache::store('redis')->set($token, $result,60000);
		return $result;
	}
	
	public function verifyToken($accessToken = ''){
		if(empty($accessToken)){
			$accessToken = self::getAccesstoken();
		}
		$userSteam = Cache::store('redis')->get($accessToken);
		if(empty($userSteam)){
			return false;
		}
		$record = UserSteam::getInfoByUserIdAndSteamId($userSteam['user_id'],$userSteam['steam_id']);

		if(empty($record)){
			return false;
		}
		return $this->saveSteamLoginInfo($record);
	}
	
	/**
	 * 获取请求的access_token值
	 * @return unknown|Ambigous <multitype:, number, multitype:unknown , boolean>|multitype:
	 */
	protected static function getAccesstoken(){
	
		$post = 	Request::instance()->post();
		$get = 	Request::instance()->get();
		$get = empty($get)?array():$get;
		$post = empty($post)?array():$post;
		$params = array_merge($post,$get);
		if(!empty($params['access_token'])){
			return $params['access_token'];
		}
		if(!empty($_REQUEST['access_token'])){
			return $_REQUEST['access_token'];
		}
	
		if(preg_match('/[\?\&]access_token=(\w+)/', $_SERVER['REQUEST_URI'], $matches)){
			return $matches[1];
		}
		
		if(!empty($_SERVER['HTTP_AUTHORIZATION'])){
			return explode(" ", $_SERVER['HTTP_AUTHORIZATION'])[1];
		}
	
	}
}